﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Web;
using ElectronicCommerce.Framework.Web.UI.GeneratedImage.ImageTransform;

namespace ElectronicCommerce.Framework.Web.UI.GeneratedImage
{
    public abstract class ImageHandler : IHttpHandler
    {
        private ImageHandlerInternal Implementation { get; set; }

        protected ImageHandler()
            : this(new ImageHandlerInternal())
        {
        }

        private ImageHandler(ImageHandlerInternal implementation)
        {
            Implementation = implementation;
        }

        /// <summary>
        /// Enables server-side caching of the result
        /// </summary>
        public bool EnableServerCache
        {
            get
            {
                return Implementation.EnableServerCache;
            }
            set
            {
                Implementation.EnableServerCache = value;
            }
        }

        /// <summary>
        /// Enables client-side caching of the result
        /// </summary>
        public bool EnableClientCache
        {
            get
            {
                return Implementation.EnableClientCache;
            }
            set
            {
                Implementation.EnableClientCache = value;
            }
        }

        /// <summary>
        /// Sets the client-side cache expiration time
        /// </summary>
        public TimeSpan ClientCacheExpiration
        {
            get
            {
                return Implementation.ClientCacheExpiration;
            }
            set
            {
                Implementation.ClientCacheExpiration = value;
            }
        }

        /// <summary>
        /// 图片所在站点虚拟路径
        /// </summary>
        public string VirtualPathRoot
        {
            get { return Implementation.VirtualPathRoot; }
            set { Implementation.VirtualPathRoot = value; }
        }

        /// <summary>
        /// Sets the type of the result image. The handler will return ouput with MIME type matching this content
        /// </summary>
        public ImageFormat ContentType
        {
            get
            {
                return Implementation.ContentType;
            }
            set
            {
                Implementation.ContentType = value;
            }
        }

        /// <summary>
        /// A list of image transforms that will be applied successively to the image
        /// </summary>
        protected List<ImageTransform.ImageTransform> ImageTransforms
        {
            get
            {
                return Implementation.ImageTransforms;
            }
        }

        public virtual ImageInfo GenerateImage(ImageGenerateRequest request)
        {
            if (!request.IsRequestOriginalImage)
            {
                ImageResizeTransform imageResizeTransform = new ImageResizeTransform();
                imageResizeTransform.Width = request.FactWidth;
                imageResizeTransform.Height = request.FactHeight;
                imageResizeTransform.Mode = request.ResizeMode;

                ImageTransforms.Add(imageResizeTransform);
            }

            return new ImageInfo(Image.FromFile(request.ImagePath));
        }

        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            PreProcessRequest(context);
            HttpContextBase contextWrapper = new HttpContextWrapper(context);
            ProcessRequest(contextWrapper);
        }

        public virtual void PreProcessRequest(HttpContext context)
        {
        }

        #endregion

        internal void ProcessRequest(HttpContextBase context)
        {
            Debug.Assert(context != null);
            Implementation.HandleImageRequest(context, delegate(ImageGenerateRequest request)
            {
                return GenerateImage(request);
            });
        }
    }
}
